package cn.iocoder.an.module.business.service.equipmentcheckrecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckrecorddetail.EquipmentCheckRecordDetailDO;
import cn.iocoder.an.module.business.dal.dataobject.equipmentmanage.EquipmentManageDO;
import cn.iocoder.an.module.business.dal.mysql.equipmentcheckrecorddetail.EquipmentCheckRecordDetailMapper;
import cn.iocoder.an.module.business.dal.mysql.equipmentmanage.EquipmentManageMapper;
import cn.iocoder.an.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.an.module.system.service.dict.DictDataService;
import cn.iocoder.an.module.system.service.user.AdminUserService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.an.module.business.controller.admin.equipmentcheckrecord.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckrecord.EquipmentCheckRecordDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.equipmentcheckrecord.EquipmentCheckRecordMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 点检记录 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class EquipmentCheckRecordServiceImpl implements EquipmentCheckRecordService {

    @Resource
    private EquipmentCheckRecordMapper equipmentCheckRecordMapper;

    @Resource
    private EquipmentCheckRecordDetailMapper equipmentCheckRecordDetailMapper;

    @Resource
    private EquipmentManageMapper equipmentManageMapper;

    @Resource
    private AdminUserService userService;

    @Resource
    private DictDataService dictDataService;


    @Override
    public Long createEquipmentCheckRecord(EquipmentCheckRecordSaveReqVO createReqVO) {
        // 插入
        EquipmentCheckRecordDO equipmentCheckRecord = BeanUtils.toBean(createReqVO, EquipmentCheckRecordDO.class);

        //
        AdminUserDO user = userService.getUser(createReqVO.getCheckUser());
        createReqVO.setCheckUserName(user.getNickname());

        equipmentCheckRecordMapper.insert(equipmentCheckRecord);
        Long recordId = equipmentCheckRecord.getId();
        List<EquipmentCheckRecordDetailDO> detailDOS = createReqVO.getDetailDO();
        for (EquipmentCheckRecordDetailDO detailDO : detailDOS) {
            detailDO.setRecordId(recordId);
        }
        if (!detailDOS.isEmpty()) {
            equipmentCheckRecordDetailMapper.insertBatch(detailDOS);
        }

        //更新一下设备的点检时间
        EquipmentManageDO equipmentManageDO = equipmentManageMapper.selectById(createReqVO.getEquipmentId());
        if (equipmentManageDO != null) {
            equipmentManageDO.setLastCheckTime(LocalDateTime.now());
            equipmentManageMapper.updateById(equipmentManageDO);
        }

        // 返回
        return equipmentCheckRecord.getId();
    }

    @Override
    public void updateEquipmentCheckRecord(EquipmentCheckRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateEquipmentCheckRecordExists(updateReqVO.getId());
        // 更新
        EquipmentCheckRecordDO updateObj = BeanUtils.toBean(updateReqVO, EquipmentCheckRecordDO.class);
        equipmentCheckRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipmentCheckRecord(Long id) {
        // 校验存在
        validateEquipmentCheckRecordExists(id);
        // 删除
        equipmentCheckRecordMapper.deleteById(id);
    }

    @Override
        public void deleteEquipmentCheckRecordListByIds(List<Long> ids) {
        // 删除
        equipmentCheckRecordMapper.deleteByIds(ids);
        }


    private void validateEquipmentCheckRecordExists(Long id) {
        if (equipmentCheckRecordMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_CHECK_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentCheckRecordDO getEquipmentCheckRecord(Long id) {
        return equipmentCheckRecordMapper.selectById(id);
    }

    @Override
    public PageResult<EquipmentCheckRecordDO> getEquipmentCheckRecordPage(EquipmentCheckRecordPageReqVO pageReqVO) {
        LambdaQueryWrapperX<EquipmentManageDO> equipmentWrapper = new LambdaQueryWrapperX<>();
        equipmentWrapper.likeIfPresent(EquipmentManageDO::getEquipmentName, pageReqVO.getEquipmentName());
        equipmentWrapper.likeIfPresent(EquipmentManageDO::getDeptName, pageReqVO.getDeptName());
        equipmentWrapper.eqIfPresent(EquipmentManageDO::getEquipmentClassify, pageReqVO.getEquipmentClassify());
        equipmentWrapper.eqIfPresent(EquipmentManageDO::getEquipmentType, pageReqVO.getEquipmentType());
        List<EquipmentManageDO> resultList = equipmentManageMapper.selectList(equipmentWrapper);
        List<Long> idList = resultList.stream()
                .map(EquipmentManageDO::getId)
                .collect(Collectors.toList());
        pageReqVO.setEquipmentIdList(idList);

        return equipmentCheckRecordMapper.selectPage(pageReqVO);
    }

}